var provinces=[];
$(window).load(function() {
	//设置表单样式
	setFormCSS();
});

$(function() {
	
	$.messager.progress({
        text: '请稍候...',
        interval: 500
    });
	
	//获取省列表
	$.ajax({
		url: PROJECT_NAME + '/province_list.do',
		type: 'POST',
		dataType: "json",
		success: function(data, status) {
			provinces=data;
			var d=new Array();
			d.push({ "text": "请选择","value": 0,"selected":true});
			$.each(data,function(index,value){
				d.push({ "text": value.name,"value": value.id});
			});
			//省列表
			$('#province').combobox({
				valueField: 'value',
				textField: 'text',
				data:d,
				onChange:function(newValue, oldValue){
					if (newValue>0) {
						$.each(provinces, function(index, value) {
							if (newValue==value.id) {
								if (!value.cities) {
									$.ajax({
										url: PROJECT_NAME + '/city_list.do',
										type: 'POST',
										data:{provinceId:newValue},
										dataType: "json",
										success: function(data, status) {
											value.cities=data;
											var cd=new Array();
											cd.push({ "text": "请选择","value": 0,"selected":true});
											$.each(value.cities,function(i,v){
												cd.push({ "text": v.name,"value": v.id});
											});
											$('#city').combobox('loadData',cd);
										},
										err: function(XMLHttpRequest, textStatus, err) {
											$.messager.alert('错误', '对不起，获取城市列表失败！', 'error');
											$.messager.progress('close');
										}
									});
								}else{
									var cd=new Array();
									cd.push({ "text": "请选择","value": 0,"selected":true});
									$.each(value.cities,function(i,v){
										cd.push({ "text": v.name,"value": v.id});
									});
									$('#city').combobox('loadData',cd);
								}
								
								return false;
							}
						});
					}else{
						$('#city').combobox('loadData',[{ "text": "请选择","value": 0,"selected":true}]);
					}
				}
			});
			//加载用户数据
			loadUserData();
			
		},
		err: function(XMLHttpRequest, textStatus, err) {
			$.messager.alert('错误', '对不起，获取省市列表失败！', 'error');
			$.messager.progress('close');
		}
	});
	
	$('#city').combobox({
		valueField: 'value',
		textField: 'text',
		data: [{ "text": "请选择","value": 0,"selected":true}]
	});
	
	
	$("#submitbtn").click(function() {
		var repwd=$("#repwd").textbox('getValue');
		if (repwd) {
			$.messager.confirm('确认重置','您确认重置该帐号密码吗？',function(r){    
				if (r){
					$("#pwd").val(repwd);
				}
				submitForm({
		    		url:PROJECT_NAME + '/user_saveorupdate.do',
		    		msg:"修改成功",
		    		action:"update"
		    	});
			}); 
		}else{
			submitForm({
	    		url:PROJECT_NAME + '/user_saveorupdate.do',
	    		msg:"修改成功",
	    		action:"update"
	    	});
		}
	});
	$("#reloadbtn").click(function(){
		loadUserData();
	});
	
});

//加载用户数据
function loadUserData(){
	$.ajax({
		url: PROJECT_NAME + '/user_getLogin.do',
		type: 'POST',
		dataType: "json",
		success: function(data, status) {
			if (data) {
				$('#fm').form('load', data);
				$("#roleName").html(data.roleName);
				//初始化省市数据
				if (data.provinceId>0) {
					$.each(provinces, function(index, value) {
						if (data.provinceId==value.id) {
							if (!value.cities) {
								$.ajax({
									url: PROJECT_NAME + '/city_list.do',
									type: 'POST',
									data:{provinceId:data.provinceId},
									dataType: "json",
									success: function(d, status) {
										value.cities=d;
										var cd=new Array();
										cd.push({ "text": "请选择","value": 0,"selected":true});
										$.each(value.cities,function(i,v){
											cd.push({ "text": v.name,"value": v.id});
										});
//										$('#province').combobox('select',data.provinceId);
										$('#city').combobox('loadData',cd);
										$('#city').combobox('select',data.cityId);
									},
									err: function(XMLHttpRequest, textStatus, err) {
										$.messager.alert('错误', '对不起，获取城市列表失败！', 'error');
									}
								});
							}else{
								var cd=new Array();
								cd.push({ "text": "请选择","value": 0,"selected":true});
								$.each(value.cities,function(i,v){
									cd.push({ "text": v.name,"value": v.id});
								});
								$('#city').combobox('loadData',cd);
								$('#city').combobox('select',row.cityId);
							}
							return false;
						}
					});
				}else{
					$('#city').combobox('loadData',[{ "text": "请选择","value": 0,"selected":true}]);
				}
			}else{
				$.messager.alert('错误', '对不起，获取用户信息失败！', 'error');
			}
			$.messager.progress('close');
		},
		err: function(XMLHttpRequest, textStatus, err) {
			$.messager.alert('错误', '对不起，获取用户信息失败！', 'error');
			$.messager.progress('close');
		}
	});
}

function submitForm(paras){
	$('#fm').form('submit', {    
	    url:paras.url,
	    queryParams:{"action":paras.action},
	    onSubmit:function(para){
	    	$.messager.progress({
                text: '请稍候...',
                interval: 500
            });
	    	var valid=$('#fm').form("validate");
	    	if (valid) {
	    		$.ajax({
	    			url: PROJECT_NAME + '/user_exist.do',
	    			type: 'POST',
	    			async:false,
	    			data:{
	    				userName:$("#uname").textbox('getValue'),
	    				userId:$("#userId").val()
	    			},
	    			dataType: "json",
	    			success: function(data, status) {
	    				if(data=="1"){
	    					valid=false;
	    					$.messager.progress('close');
	    					$.messager.alert('错误', '对不起，用户名已存在！', 'error');
	    				}
	    			},
	    			err: function(XMLHttpRequest, textStatus, err) {
	    				$.messager.progress('close');
	    				$.messager.alert('错误', '对不起，验证用户名失败！', 'error');
	    			}
	    		});
			}else{
				$.messager.progress('close');
			}
	    	 return valid;
	    },
	    success:function(data){
	    	$.messager.progress('close');
	        if(parseInt(data)>0){
	        	$('#dlg').dialog('close');
	        	showMsgBox(paras.msg);
	        }else{
	        	$.messager.alert("错误", "操作失败！请与系统管理员联系!", 'error');
	        }
	    }    
	});
}

